package com.fireline.beauty.cashier;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashSet;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

import com.fireline.beauty.jdbc.SqliteJdbcImpl;
import com.zgw.fireline.base.controls.DataTable;
import com.zgw.fireline.base.controls.DataTableColumn;
import com.zgw.fireline.base.dataset.Dataset;
import com.zgw.fireline.base.frame.FunctionDialog;
import com.zgw.fireline.base.frame.IFunction;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.wb.swt.SWTResourceManager;
import com.zgw.fireline.base.IDataBaseProvide;

/**
 * 会员消费视图
 * */
public class HyxfView implements IFunction {
	/**
	 * @wbp.nonvisual location=27,472
	 */
	private final Dataset hyxfSet = new Dataset(
			"select * from hyxf where hybh=?", SqliteJdbcImpl.INSTANCE);
	/**
	 * @wbp.nonvisual location=137,482
	 */
	private final Dataset hyxfmxSet = new Dataset(
			"\r\nselect a.id,case\r\n\t when a.lx=1 then\r\n\t'护理消费'\r\n        when a.lx=2 then\r\n\t'产品消费'\r\n        when a.lx=3 then\r\n\t'套餐消费'\t\r\n\tend xflx,\r\n\tifnull(b.xmmc,c.cpmc) mc,\r\n\ta.dj,\r\n\ta.cs,\r\n\t(a.dj*a.cs) je,\r\n\ta.jf\r\n from hyxfnr a \r\nleft join fwxm b on a.lx in (1,3) and bh=b.xmbh\r\nleft join cpxx c on a.lx=2 and bh=c.cpbh\r\nwhere xfid=?",
			SqliteJdbcImpl.INSTANCE);
	private String hybh;
	private Shell shell;
	private CLabel labXfze;
	/**
	 * @wbp.nonvisual location=237,502
	 */
	private final Dataset hyxxSet = new Dataset(
			"select * from hyxx where hybh=?", SqliteJdbcImpl.INSTANCE);

	/**
	 * @wbp.parser.entryPoint
	 */
	@Override
	public Control createControl(Composite parent) {
		this.shell = parent.getShell();
		hyxfSet.setKeyColumns(new String[] { "xfid" });
		hyxfmxSet.setKeyColumns(new String[] {});
		hyxxSet.setKeyColumns(new String[] {});
		Composite content = new Composite(parent, SWT.NONE);
		content.setLayout(new GridLayout(1, false));

		Composite composite = new Composite(content, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		GridLayout gl_composite = new GridLayout(3, false);
		gl_composite.marginHeight = 10;
		gl_composite.marginWidth = 50;
		composite.setLayout(gl_composite);
		ToolBar toolBar = new ToolBar(composite, SWT.FLAT | SWT.RIGHT);
		ToolItem tltmf = new ToolItem(toolBar, SWT.NONE);
		tltmf.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doTjXf();
			}
		});
		tltmf.setText("添加消费（F2）");

		ToolItem toolItem = new ToolItem(toolBar, SWT.SEPARATOR);

		ToolItem tltmCheckItem = new ToolItem(toolBar, SWT.NONE);
		tltmCheckItem.setText("解挂");
		tltmCheckItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doJg();
			}
		});

		ToolItem toolItem_2 = new ToolItem(toolBar, SWT.SEPARATOR);

		ToolItem toolItem_1 = new ToolItem(toolBar, SWT.NONE);
		toolItem_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doHk();
			}
		});
		toolItem_1.setText("还款");

		Label lblNewLabel = new Label(composite, SWT.NONE);
		GridData gd_lblNewLabel = new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 1);
		gd_lblNewLabel.horizontalIndent = 50;
		lblNewLabel.setLayoutData(gd_lblNewLabel);
		lblNewLabel.setText("消费总额：");

		labXfze = new CLabel(composite, SWT.NONE);
		labXfze.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		labXfze.setFont(SWTResourceManager.getFont("宋体", 10, SWT.BOLD));
		labXfze.setText("");

		SashForm sashForm = new SashForm(content, SWT.NONE);
		sashForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1,
				1));

		DataTable dataTable = new DataTable(sashForm, SWT.BORDER
				| SWT.FULL_SELECTION, hyxfSet);
		dataTable.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doSelectedXfd();
			}
		});
		dataTable.setHeaderVisible(true);
		dataTable.setLinesVisible(true);

		DataTableColumn dataTableColumn = new DataTableColumn(dataTable,
				SWT.NONE, "rq");
		dataTableColumn.setText("日期");
		dataTableColumn.setWidth(80);

		DataTableColumn dtblclmnXfid = new DataTableColumn(dataTable, SWT.NONE,
				"xfid");
		dtblclmnXfid.setText("消费单号");
		dtblclmnXfid.setWidth(80);

		DataTableColumn dataTableColumn_1 = new DataTableColumn(dataTable,
				SWT.RIGHT, "xfje");
		dataTableColumn_1.setText("消费金额");
		dataTableColumn_1.setWidth(80);

		DataTableColumn dataTableColumn_2 = new DataTableColumn(dataTable,
				SWT.RIGHT, "xj");
		dataTableColumn_2.setText("现金");
		dataTableColumn_2.setWidth(80);

		DataTableColumn dataTableColumn_3 = new DataTableColumn(dataTable,
				SWT.RIGHT, "czk");
		dataTableColumn_3.setText("储值卡");
		dataTableColumn_3.setWidth(80);

		DataTableColumn dataTableColumn_4 = new DataTableColumn(dataTable,
				SWT.RIGHT, "djq");
		dataTableColumn_4.setText("代金券");
		dataTableColumn_4.setWidth(80);

		DataTableColumn dataTableColumn_5 = new DataTableColumn(dataTable,
				SWT.RIGHT, "xyk");
		dataTableColumn_5.setText("信用卡");
		dataTableColumn_5.setWidth(80);

		DataTableColumn dataTableColumn_15 = new DataTableColumn(dataTable,
				SWT.RIGHT, "gz");
		dataTableColumn_15.setText("欠款金额");
		dataTableColumn_15.setWidth(80);

		DataTableColumn dataTableColumn_6 = new DataTableColumn(dataTable,
				SWT.NONE, "jf");
		dataTableColumn_6.setText("所得积分");
		dataTableColumn_6.setWidth(80);

		DataTableColumn dataTableColumn_7 = new DataTableColumn(dataTable,
				SWT.NONE, "czy");
		dataTableColumn_7.setText("操作人");
		dataTableColumn_7.setWidth(80);

		DataTableColumn dataTableColumn_8 = new DataTableColumn(dataTable,
				SWT.NONE, "bz");
		dataTableColumn_8.setText("备注");
		dataTableColumn_8.setWidth(80);

		DataTable tableXfmx = new DataTable(sashForm, SWT.BORDER
				| SWT.FULL_SELECTION, hyxfmxSet);
		tableXfmx.setHeaderVisible(true);
		tableXfmx.setLinesVisible(true);

		DataTableColumn dataTableColumn_9 = new DataTableColumn(tableXfmx,
				SWT.NONE, "xflx");
		dataTableColumn_9.setText("消费类型");
		dataTableColumn_9.setWidth(80);

		DataTableColumn dataTableColumn_10 = new DataTableColumn(tableXfmx,
				SWT.NONE, "mc");
		dataTableColumn_10.setText("名称");
		dataTableColumn_10.setWidth(80);

		DataTableColumn dataTableColumn_11 = new DataTableColumn(tableXfmx,
				SWT.NONE, "dj");
		dataTableColumn_11.setText("单价");
		dataTableColumn_11.setWidth(80);

		DataTableColumn dataTableColumn_12 = new DataTableColumn(tableXfmx,
				SWT.NONE, "cs");
		dataTableColumn_12.setText("次数");
		dataTableColumn_12.setWidth(80);

		DataTableColumn dataTableColumn_13 = new DataTableColumn(tableXfmx,
				SWT.NONE, "je");
		dataTableColumn_13.setText("金额");
		dataTableColumn_13.setWidth(80);

		DataTableColumn dataTableColumn_14 = new DataTableColumn(tableXfmx,
				SWT.NONE, "jf");
		dataTableColumn_14.setText("积分");
		dataTableColumn_14.setWidth(80);
		sashForm.setWeights(new int[] { 1, 1 });
		setParams();
		return content;
	}

	/*
	 * 解除会员消费挂单
	 */
	protected void doJg() {
		GdDialog dlg = new GdDialog(shell, hybh);
		if (dlg.open() == SWT.CHANGED) {
			refresh(hybh);
		}
	}

	/**
	 * 还款
	 * */
	protected void doHk() {
		if (hyxfSet.getSelectedIndex() != -1 && hyxfSet.getValue("gz") != null
				&& hyxfSet.getValue("gz", Double.class) > 0) {
			String xfid = hyxfSet.getString("xfid");
			HkDialog hk = new HkDialog(shell, xfid);
			if (hk.open() == SWT.OK) {
				refresh(hybh);
				hyxfSet.setSelected(xfid);
			}
		} else {
			MessageDialog.openInformation(shell, "提示", "选择一条欠款大于0的数据");
		}
	}

	/*
	 * 添加消费
	 */
	protected void doTjXf() {
		HyxfEdit fun = new HyxfEdit(hybh);
		FunctionDialog dlg = new FunctionDialog(shell, fun);
		dlg.open();
		if (fun.result == SWT.OK) {
			refresh(hybh);
		}
	}

	/*
	 * 选择消费单 刷新消费明细
	 */
	protected void doSelectedXfd() {
		if (hyxfSet.getSelectedIndex() != -1) {
			hyxfmxSet.setParam(0, hyxfSet.getValue("xfid"));
			hyxfmxSet.update();
		}
	}

	/**
	 * 设置会员编号 刷新数据
	 * */
	public void refresh(String hybh) {
		this.hybh = hybh;
		hyxfSet.clear();
		hyxfmxSet.clear();

		// 更新消费信息
		hyxfSet.setParam(0, hybh);
		hyxfSet.update();

		// 更新消费明细
		if (hyxfSet.getCount() > 0) {
			hyxfSet.setSelected(0);
			hyxfmxSet.setParam(0, hyxfSet.getValue("xfid"));
			hyxfmxSet.update();
		}

		// 设置消费总额信息
		hyxxSet.setParam(0, hybh);
		hyxxSet.update();
		hyxxSet.setSelected(0);
		labXfze.setText(DecimalFormat.getCurrencyInstance().format(
				hyxxSet.getValue("xfje", Double.class, 0D)));
		labXfze.getParent().layout();
	}

	protected void setParams() {
	}

	public static void main(String[] args) {
		System.out.println(new java.sql.Date(0));
	}
}
